package model;

import java.util.HashMap;
import java.util.Map;

import model.impl.LectureServiceImpl;

public class ServiceFactory
{
	private static ServiceFactory instance = null;
	private java.util.Map<String, Service> services;
	public static ServiceFactory getInstance()
	{
		if( instance == null )
		{
			instance = new ServiceFactory();
		}
		return instance;
	}
	private ServiceFactory()
	{
		services = new HashMap<String, Service>();
	}
	
	
	public <T> T getService(Class<T> s) throws ClassNotFoundException
	{
		String name = s.getSimpleName();
		if(services.containsKey(name))
		{
			System.out.println(name + " is created!");
			return (T)services.get(name);
		}
		else
		{	
			String implName = "model.impl." + name + "Impl";
			
			System.out.println(implName);
			try
			{
				Service ss = (Service)Class.forName(implName).newInstance();				
				System.out.println(implName + "created, putting in map!");
				services.put(name, ss);
				return (T)ss;
			}
			catch(IllegalAccessException e)
			{
				e.printStackTrace();
			}
			catch(InstantiationException e)
			{
				e.printStackTrace();
			}
		}
		return null;
	}
}
